﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestCenter.Fit
{
    class CulFitting
    {
         #region  得到数据的法矩阵,输出为发矩阵的增广矩阵
        static public double[,] Get_Array(double[] y, double[] x, int n)
        {
            double[,] result = new double[n + 1, n + 2];

            if (y.Length != x.Length)
            {
                throw (new Exception("两个输入数组长度不一！"));
                //return null;
            }

            for (int i = 0; i <= n; i++)
            {
                for (int j = 0; j <= n; j++)
                {
                    result[i, j] = Cal_sum(x, i + j);
                }
                result[i, n + 1] = Cal_multi(y, x, i);
            }

           return result;
        }

        #endregion

         #region 累加的计算
        static private double Cal_sum(double[] input,int order)
        {
            double result=0;
            int length = input.Length;          

            for (int i = 0; i < length; i++)
            {
                result += Math.Pow(input[i], order);
            }

           return result;
        }
        #endregion

         #region 计算∑(x^j)*y
        static private double Cal_multi(double[] y,double[] x,int order)
        {
            double result = 0;

            int length = x.Length;

            for (int i = 0; i < length; i++)
            {
                result += Math.Pow(x[i], order) * y[i];
            }

            return result;
        }
         #endregion
    }
}
